<?php
	class CommonQuery
	{
		private $db;
	
		public function __construct()
		{
			$this->db = new dataBase();
		}
		public function activeElemant($table_name,$id_field,$id=false,$itemId=false)
		{
			if(isset($id))
			{
				$sQuery = "UPDATE $table_name SET flag_active=true WHERE $id_field=?";
				$aResult = $this -> db -> ModifyQuery($sQuery,array($id),true);
			}
			else
			{
				foreach($itemId as $key=>$value)
				{
					$sQuery = "UPDATE $table_name SET flag_active=true WHERE $id_field=?";
					$aResult = $this -> db -> ModifyQuery($sQuery,array($value));
				}
			}
		}
		public function desactiveElemant($table_name,$id_field,$id=false,$itemId=false)
		{
			if(isset($id))
			{
				$sQuery = "UPDATE $table_name SET flag_active=false WHERE $id_field=?";
				$aResult = $this -> db -> ModifyQuery($sQuery,array($id),true);
			}
			else
			{
				foreach($itemId as $key=>$value)
				{
					$sQuery = "UPDATE $table_name SET flag_active=false WHERE $id_field=?";
					$aResult = $this -> db -> ModifyQuery($sQuery,array($value));
					
				}
			}
		}
		public function deleteElemant($table_name,$id_field,$id=false,$itemId=false)
		{
			if(isset($id))
			{
				$sQuery = "UPDATE $table_name SET flag_trash=true WHERE $id_field=?";
				$aResult = $this -> db -> ModifyQuery($sQuery,array($id));
			}
			else
			{
				foreach($itemId as $key=>$value) 
				{
					$sQuery = "UPDATE $table_name SET flag_trash=true WHERE $id_field=?";
					$aResult = $this -> db -> ModifyQuery($sQuery,array($value));
					
				}
			}
		}
		public function getElementById($table_name, $id_field, $id)
		{
			//echo "tak";exit;
			$sQuery = "SELECT * FROM $table_name WHERE $id_field=? AND flag_trash=false";
			$aResult = $this -> db -> ExecuteQuery($sQuery,array($id));
			
			return $aResult;
		}
		public function getAllElements($table_name)
		{
			$sQuery = "SELECT * FROM $table_name WHERE flag_trash=false";
			$aResult = $this -> db -> ExecuteQuery($sQuery,array(),true);
			return $aResult;
		}
		public function getStates()
		{
			$sQuery = "SELECT * FROM states";
			$aResult = $this -> db -> ExecuteQuery($sQuery,array());
			
			return $aResult;
		}
		public function getElementByName($table_name, $name)
		{
			$sQuery = "SELECT * FROM $table_name WHERE name=?";
			$aResult = $this -> db -> ExecuteQuery($sQuery,array($name),true);
			
			//print_r($aResult);exit;
			if(isset($aResult[0][0])&&$aResult[0][0]!="")
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public function getElementByLogin($table_name, $login)
		{
			$sQuery = "SELECT * FROM $table_name WHERE login=?";
			$aResult = $this -> db -> ExecuteQuery($sQuery,array($login),true);
			//echo $sQuery; 
			//echo $login;
			//print_r($aResult);exit;
			if(isset($aResult[0][0])&&$aResult[0][0]!="")
			{
				return true;
			}
			else
			{
				return false;
			}
		}

                /**
                 * Pobieranie elementu po polach
                 */
                public function select($table_name,$what,$fields=null,$order="")
                {
                    $get = $what;
//                    pre($what);
                    if($what != "*" && count($what) > 1)
                    {
                        $get = implode(',', $what);
                    }
                    elseif(isset($what[0]) && is_array($what))
                    {
                        $get = $what[0];
                    }
                    
                    $where = $this -> where($fields);
//                    pre($fields);
                    $query = "SELECT $get FROM $table_name ".$where[0]." ".$order;
//                    echo $query;exit;
                    $aResult = $this -> db -> PdoQuery($query, $where[1]);

                    return $aResult;
                }

                /**
                 * Ustawianie parametrów do zapytania
                 */
                public function where($fields)
                {
                    $values = array();
                    if($fields != null)
                    {
                        $whereTemp = array();
                        $values = array();
                        foreach($fields as $key => $value)
                        {
                            array_push($whereTemp, $key."=?");
                            array_push($values, $value);
                        }

                        $where = implode(" AND ", $whereTemp);
                        $where = "WHERE ".$where;
                    }
                    else
                    {
                        $where = "";
                    }

                    return array($where,$values);
                }

                /**
                 * Zapisywanie elementu do bazy
                 * @param <type> $elements
                 * @param <type> $tableName
                 * @return <type>
                 */
                public function save($elements,$tableName)
                {
                    $sQuery = "INSERT INTO $tableName (";

                    $questionMarks = array();
                    $values = array();
                    $fields = array();

                    foreach($elements as $key=>$value)
                    {
                        if($value == "now()")
                        {
                            array_push($questionMarks, 'now()');
                        }
                        else
                        {
                            array_push($questionMarks, '?');
                            array_push($values, $value);
                        }
                        array_push($fields, $key);
                    }
                    $fields = implode(',', $fields);
                    $questionMarks = implode(',', $questionMarks);

                    $sQuery .= "$fields ) VALUES ($questionMarks";
                    $sQuery .= " )";

                    $aResult = $this -> db -> ModifyQuery($sQuery, $values);

                    return $aResult;
                }

                /**
                 *  Aktualizacja elementu
                 * @param <type> $elements
                 * @param <type> $tableName
                 * @param <type> $where
                 * @return <type>
                 */
                public function update($elements,$tableName,$where=null)
                {
                    $sQuery = "UPDATE $tableName SET";

                    $questionMarks = array();
                    $values = array();
                    $fields = array();

                    foreach($elements as $key=>$value)
                    {
                        if($value == "now()")
                        {
                            array_push($fields,$key.'=now()');
                        }
                        else
                        {
                            array_push($values, $value);
                            array_push($fields, $key.'=?');
                        }
                    }
                    $fields = implode(',', $fields);

                    $sQuery = "UPDATE $tableName SET $fields";


                    if($where != null)
                    {
                        if(is_array($where))
                        {
                            $sQuery.= " WHERE ";
                            $countNew = count($where);
                            $i = 0;
                            foreach($where as $key => $value)
                            {
                                $sQuery.= $key." = ? ";
                                array_push($values, $value);
                                $i++;
                                if($i != $countNew)
                                {
                                    $sQuery.= " AND ";    
                                }
                            }
                        }
                        else
                        {
                            
                            $sQuery.=" WHERE ".$where;                            
                        }
                    }
//                    pre($values);
                    $aResult = $this -> db -> ModifyQuery($sQuery, $values);

                    return $aResult;
                }
	}
?>